Displaying items by tag: doc cpp Page Setup 您所在的位置:网站首页 insert page breaks Displaying items by tag: doc cpp Page Setup

Displaying items by tag: doc cpp Page Setup

#Displaying items by tag: doc cpp Page Setup| 来源: 网络整理| 查看: 265

In MS Word, a Page Break is an important feature of page layout that helps you start a new page wherever you want. After inserting page breaks, all formatting of the previous page applies to the new page, which makes the whole document neat and well organized. In this article, you will learn how to programmatically add or remove page breaks in a Word document using Spire.Doc for C++.

Insert a Page Break after a Specific Paragraph in Word in C++ Insert a Page Break after a Specific Text in Word in C++ Remove Page Breaks in a Word Document in C++ Install Spire.Doc for C++

There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.Doc for C++ in a C++ Application

Insert a Page Break after a Specific Paragraph in Word in C++

Spire.Doc for C++ offers the Paragraph->AppendBreak(BreakType::PageBreak) method to insert a page break after a paragraph. Once inserted, a symbol indicating the page break will be shown. The following are the detailed steps.

Create a Document instance. Load a Word document using Document->LoadFromFile() method. Get a specified section using Document->GetSections()->GetItem(sectionIndex) method. Get a specified paragraph using Section->GetParagraphs()->GetItem(paragraphIndex) method. Add a page break to end of the paragraph using Paragraph->AppendBreak(BreakType::PageBreak) method. Save the result document using Document->SaveToFile() method. C++ #include "Spire.Doc.o.h" using namespace Spire::Doc; int main() { //Specify input file path and name std::wstring input_path = L"Data\\"; std::wstring inputFile = input_path + L"Input.docx"; //Specify output file path and name std::wstring output_path = L"Output\\"; std::wstring outputFile = output_path + L"InsertPageBreak.docx"; //Create a Document instance Document* document = new Document(); //Load a Word document from disk document->LoadFromFile(inputFile.c_str()); //Get the first section Section* section = document->GetSections()->GetItem(0); //Get the 2nd paragraph in the section Paragraph* paragraph = section->GetParagraphs()->GetItem(1); //Insert a page break after the paragraph paragraph->AppendBreak(BreakType::PageBreak); //Save the result document document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013); document->Close(); delete document; }

C++: Insert or Remove Page Breaks in Word

Insert a Page Break after a Specific Text in Word in C++

In addition to inserting a page break after a paragraph, Spire.Doc for C++ also allows you to find a specified text and then insert the page break after the specified text. The following are the detailed steps.

Create a Document instance. Load a Word document using Document->LoadFromFile() method. Find a specified text using Document->FindString() method. Get the text range of the specified text using TextSelection->GetAsOneRange() method. Get the paragraph where the text range is located using TextRange->GetOwnerParagraph() method. Get the position index of the text range in the paragraph using Paragraph->GetChildObjects()->IndexOf() method. Initialize an instance of Break class to create a page break. Insert the page break after the specified text using Paragraph->GetChildObjects()->Insert() method. Save the result document using Document->SaveToFile() method. C++ #include "Spire.Doc.o.h" using namespace Spire::Doc; int main() { //Specify input file path and name std::wstring input_path = L"Data\\"; std::wstring inputFile = input_path + L"Input.docx"; //Specify output file path and name std::wstring output_path = L"Output\\"; std::wstring outputFile = output_path + L"InsertPageBreakAfterText.docx"; //Create a Document instance Document* document = new Document(); //Load a Word document from disk document->LoadFromFile(inputFile.c_str()); //Find a specified text TextSelection* selection = document->FindString(L"infrastructure", true, true); //Get the text range of the specified text TextRange* range = selection->GetAsOneRange(); //Get the paragraph where the text range is located Paragraph* paragraph = range->GetOwnerParagraph(); //Get the position index of the text range in the paragraph int index = paragraph->GetChildObjects()->IndexOf(range); //Create a page break Break* pageBreak = new Break(document, BreakType::PageBreak); //Insert a page break after the specified text paragraph->GetChildObjects()->Insert(index + 1, pageBreak); //Save to result document document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013); document->Close(); delete document; }

C++: Insert or Remove Page Breaks in Word

Remove Page Breaks in a Word Document in C++

Some mistakenly added page breaks can mess up the structure of your entire document, so it's quite necessary to remove them. The following are the steps to remove page breaks in a Word document.

Create a Document instance. Load a Word document using Document->LoadFromFile() method. Traverse through each paragraph in the first section, and then traverse through each child object of a paragraph. Determine whether the child object type is a page break. If yes, remove the page break from the paragraph using Paragraph->GetChildObjects()->Remove() method. Save the result document using Document->SaveToFile() method. C++ #include "Spire.Doc.o.h" using namespace Spire::Doc; int main() { //Specify input file path and name std::wstring input_path = L"Data\\"; std::wstring inputFile = input_path + L"InsertPageBreak.docx"; //Specify output file path and name std::wstring output_path = L"Output\\"; std::wstring outputFile = output_path + L"RemovePageBreaks.docx"; //Create a Document instance Document* document = new Document(); //Load a Word document from disk document->LoadFromFile(inputFile.c_str()); //Traverse through each paragraph in the first section of the document for (int j = 0; j < document->GetSections()->GetItem(0)->GetParagraphs()->GetCount(); j++) { Paragraph* p = document->GetSections()->GetItem(0)->GetParagraphs()->GetItem(j); //Traverse through each child object of a paragraph for (int i = 0; i < p->GetChildObjects()->GetCount(); i++) { DocumentObject* obj = p->GetChildObjects()->GetItem(i); //Determine whether the child object type is a page break if (obj->GetDocumentObjectType() == DocumentObjectType::Break) { Break* b = dynamic_cast(obj); //Remove the page break from the paragraph p->GetChildObjects()->Remove(b); } } } //Save the result document document->SaveToFile(outputFile.c_str(), FileFormat::Docx2013); document->Close(); delete document; }

C++: Insert or Remove Page Breaks in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有